<?php

namespace App\Service\OpenShop\Feishu;

use App\Enums\OpenShop\InvestmentPromotion\ErrCode;
use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\InvestmentPromotion\OpenShopCacheEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\UserIdentityCardEnum;
use App\Events\OpenShop\OpenShopProcessEvent;
use App\Events\OpenShop\SignSmsSentEvent;
use App\Exceptions\Custom\CustomHttpResponseException;
use App\Http\Resources\OpenShop\InvestmentPromotion\OrderDetailResource;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Service\Common\ShortUrlService;
use App\Service\Common\SmsService;
use Illuminate\Support\Facades\Cache;

class SendSmsService
{
    private string $orderNo;
    private string $phone;
    private string $name;

    private ?OpenShopSignOrderModel $signOrderModel;

    const SMS_TYPE_APP = 'APP';
    const SMS_TYPE_WAP = 'WAP';

    public function __construct(string $orderNo)
    {
        $this->orderNo        = $orderNo;
        $this->signOrderModel = OpenShopSignOrderModel::query()->where('orderNo', $orderNo)->first();
        if (!$this->signOrderModel) {
            throw new CustomHttpResponseException(ErrCode::errCodeTrans(ErrCode::CODE_SIGN_ORDER_NO_NULL));
        }

        $this->phone = (string)$this->signOrderModel->baseInfoRel->mobile;
        $this->name  = (string)$this->signOrderModel->identityCardRel->name;
    }

    /**
     * 发送支付短信
     * @param string $orderNo
     * @param $userInfo
     * @param string $smsType
     * @return string
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
     */
    public function sendSmsBySign($userInfo, string $smsType)
    {
        if ($this->signOrderModel->step < SignOrderEnum::STEP_2 || $this->signOrderModel->step >= SignOrderEnum::STEP_4) {
            return '';
        }

        if ($smsType == self::SMS_TYPE_APP) {
            $this->appPaySms();
        } elseif ($smsType == self::SMS_TYPE_WAP) {
            $this->wapPaySms();
        } else {
            throw new CustomHttpResponseException(ErrCode::errCodeTrans(ErrCode::CODE_ERR_SMS_TYPE));
        }

        //通过订单号获取主流程ID
        event(new SignSmsSentEvent(
            $this->orderNo,
            $userInfo->id
        ));

        return '发送成功';
    }

    /**
     * app支付短信
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
     */
    private function appPaySms(): void
    {
        if (Cache::get(OpenShopCacheEnum::getShopManagerPwd($this->phone))) {

            // 新加盟商
            $smsTemplate = config('sms.template4');

            $params = [
                'name'     => $this->name,
                'order'    => $this->orderNo,
                'account'  => $this->phone,
                'password' => Cache::get(OpenShopCacheEnum::getShopManagerPwd($this->phone)),
            ];
        } else {

            // 老加盟商
            $smsTemplate = config('sms.template3');

            $params = [
                'name'  => $this->name,
                'order' => $this->orderNo,
            ];
        }

        $sms = new SmsService();
        $sms->aliyunSend($this->phone, $smsTemplate, $params);
    }

    /**
     * wap支付短信
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
     */
    private function wapPaySms(): void
    {
        //生成短链
        $params = [
            'name'  => $this->name,
            'order' => $this->orderNo,
            'code'  => ShortUrlService::addUrl(OrderDetailResource::formatSignUrl($this->orderNo))['code'],
        ];
        $sms    = new SmsService();
        $sms->aliyunSend($this->phone, config('sms.template0'), $params);
    }

    /**
     * 设计、设备、软装模块催付
     * @return void
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
     */
    public function noticePay(string $template): void
    {
        $params = [
            'name'     => $this->name,
            'shopCode' => $this->signOrderModel->kfShopCode,
        ];

        (new SmsService())->aliyunSend($this->phone, $template, $params);
    }
}
